﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head><title>Wrappanel sample</title><style type="text/css"> html, body {background-color:Black; } </style>
<script type="text/javascript">window.DLR = {}; DLR.path = "/dlr"; DLR.settings = { background: 'black' };</script>
<script src="/dlr/dlr.js" type="text/javascript"></script></head><body>
<script type="text/python" class="wrap">
from System import *
from System.Net import *
from System.Windows import * 
from System.Windows.Controls import *
from System.Windows.Input import *
from System.Windows.Media import *
from System.Windows.Shapes import *
from System.Windows.Media.Animation import *
from System.Windows.Media.Imaging import * # for bitmap
from System.Net import * # for WebClient
from System.Windows.Resources import * # for WebClient
from System.Diagnostics import * # enables outputing to a debug window
from System.Windows.Markup import * # for XamlReader
from System.Collections.Generic import *
from System.Collections.ObjectModel import *
from Microsoft.Scripting.Silverlight.DynamicApplication import MakeUri

class WrapPanel(Panel):
    def __init__(self, xaml):
        self.Content = XamlReader.Load(xaml)
           
    def MeasureOverride(self, availableSize):
        sizeSoFar = Size(0, 0)
        maxWidth = 0.0
        for i in range( 0, self.Children.Count):
            self.Children[i].Measure(Size(500, 500))
            
            if sizeSoFar.Width + self.Children[i].DesiredSize.Width > availableSize.Width:
                sizeSoFar.Height += self.Children[i].DesiredSize.Height
                sizeSoFar.Width = 0
            else:
                sizeSoFar.Width += self.Children[i].DesiredSize.Width
                maxWidth = Math.Max(sizeSoFar.Width, maxWidth)

        return Size(maxWidth, sizeSoFar.Height);
        
    def ArrangeOverride(self, finalSize):
        sizeSoFar = Size(0, 0)
        
        for i in range( 0, self.Children.Count):
            self.Children[i].Arrange(Rect(sizeSoFar.Width, sizeSoFar.Height, self.Children[i].DesiredSize.Width, self.Children[i].DesiredSize.Height))
            if sizeSoFar.Width + self.Children[i].DesiredSize.Width >= finalSize.Width:
                sizeSoFar.Height += self.Children[i].DesiredSize.Height
                sizeSoFar.Width = 0
            else:
                sizeSoFar.Width += self.Children[i].DesiredSize.Width

        return super(Panel, self).ArrangeOverride(finalSize) #same as base.ArrangeOverride
    
def Loaded(sender, e):
    wrapPanel = WrapPanel(e.Result)
    me.LayoutRoot.Children.Add(wrapPanel)
    
    for i in range( 0, 10):
        c1 = Canvas()
        c1.Width = 100;
        c1.Height = 100;
        c1.Margin = Thickness(2)
        c1.Background = SolidColorBrush(Colors.Red)
        wrapPanel.Children.Insert(0, c1)
        
        c2 = Canvas()
        c2.Width = 100;
        c2.Height = 100;
        c2.Margin = Thickness(2)
        c2.Background = SolidColorBrush(Colors.Green)
        wrapPanel.Children.Insert(0, c2)

def Load():
    client = WebClient()
    client.DownloadStringCompleted += Loaded
    client.DownloadStringAsync(MakeUri("wrappanel.xaml"))

Load()
</script>

<script id="wrap" type="application/xml+xaml" width="100%" height="1000">
    <Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Grid x:Name="LayoutRoot" Background="Black"  >
        </Grid>
    </Grid>
</script>
</body>
</html>
